package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hospital.common.core.domain.entity.SysDictData;
import com.hospital.common.core.domain.entity.SysUser;
import com.hospital.common.core.domain.model.LoginUser;
import com.hospital.common.helper.LoginHelper;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.system.mapper.SysDictDataMapper;
import com.hospital.system.mapper.SysUserMapper;
import com.hospital.workstation.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ClinicHandbookBo;
import com.hospital.workstation.domain.vo.ClinicHandbookVo;
import com.hospital.workstation.domain.ClinicHandbook;
import com.hospital.workstation.mapper.ClinicHandbookMapper;
import com.hospital.workstation.service.IClinicHandbookService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 临床诊疗指南Service业务层处理
 *
 * @author whb
 * @date 2023-03-23
 */
@RequiredArgsConstructor
@Service
public class ClinicHandbookServiceImpl implements IClinicHandbookService {

    private final ClinicHandbookMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysDictDataMapper dictDataMapper;

    /**
     * 查询临床诊疗指南
     */
    @Override
    public ClinicHandbookVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询临床诊疗指南列表
     */
    @Override
    public TableDataInfo<ClinicHandbookVo> queryPageList(ClinicHandbookBo bo, PageQuery pageQuery) {
        // 查询诊疗指南类别列表
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataByType(DictConstants.HANDBOOK_CATEGORY_DICT_TYPE);
        Map<String, String> handbookCategoryMap = sysDictDataList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 查询诊疗指南类别列表
        LambdaQueryWrapper<ClinicHandbook> lqw = buildQueryWrapper(bo);
        Page<ClinicHandbookVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 设置诊疗指南类别名称
        result.getRecords().stream().forEach(clinicHandbookVo -> clinicHandbookVo.setHandbookCategoryStr(handbookCategoryMap.get(clinicHandbookVo.getHandbookCategory())));
        return TableDataInfo.build(result);
    }

    /**
     * 查询临床诊疗指南列表
     */
    @Override
    public List<ClinicHandbookVo> queryList(ClinicHandbookBo bo) {
        LambdaQueryWrapper<ClinicHandbook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClinicHandbook> buildQueryWrapper(ClinicHandbookBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClinicHandbook> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getHandbookContent()), ClinicHandbook::getHandbookContent, bo.getHandbookContent());
        lqw.eq(StringUtils.isNotBlank(bo.getHandbookCategory()), ClinicHandbook::getHandbookCategory, bo.getHandbookCategory());
        lqw.orderByDesc(ClinicHandbook::getCreateTime);
        return lqw;
    }

    /**
     * 新增临床诊疗指南
     */
    @Override
    public Boolean insertByBo(ClinicHandbookBo bo) {
        // 获取登录人信息
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            return false;
        }
        // 查询登录人姓名
        SysUser sysUser = userMapper.selectUserById(loginUser.getUserId());
        if (ObjectUtil.isNull(sysUser)) {
            return false;
        }
        // 保存
        ClinicHandbook add = BeanUtil.toBean(bo, ClinicHandbook.class);
        add.setCreateBy(sysUser.getNickName());
        add.setUpdateBy(sysUser.getUpdateBy());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改临床诊疗指南
     */
    @Override
    public Boolean updateByBo(ClinicHandbookBo bo) {
        ClinicHandbook update = BeanUtil.toBean(bo, ClinicHandbook.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ClinicHandbook entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除临床诊疗指南
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
